<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="30%"
      :before-close="handleClose">
      <div>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="名称" prop="good_code_name" style="width: 50%">
            <el-input v-model="ruleForm.good_code_name" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="上级分类" prop="parent_cate_id">
            <el-select v-model="ruleForm.parent_cate_id" placeholder="请选择上级分类">
              <el-option label="顶级分类" value="0"></el-option>
              <el-option v-for="item in classifyOptions" :label="item.title" :value="item.id" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="菜单排序" prop="sort">
            <el-input v-model="ruleForm.sort" type="number" oninput="if(value<1)value=1" placeholder="请输入菜单排序"/>
          </el-form-item>
        </el-form>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitClick('ruleForm')">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { addLiangList } from '../../../../../../api/personalizedMall/goodNumberType'

const statusObj = {
  ADD: 'add',
  EDIT: 'edit'
}

export default {
  name: "categoryManagementModal",
  props: {
    classifyOptions: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      status: statusObj.ADD,
      visible: false,
      title: '',
      ruleForm: {
        parent_cate_id: null, // 上级分类id，顶级分类传0，必须
        good_code_name: null,   // 靓号名称，必须
        sort: null              // 排序  必填
      },
      rules: {
        good_code_name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        parent_cate_id: [
          { required: true, message: '请选择上级分类', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请选择上级分类', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    openModal(data) {
      if (data) {
        this.title = '编辑靓号分类'
        this.status = statusObj.EDIT
      } else {
        this.title = '新增靓号分类'
        this.status = statusObj.ADD
      }

      this.visible = true
    },
    /**
     * submitClick
     * @desc 点击提交按钮
     */
    submitClick(formName) {
      this.$refs[formName].validate(async (valid) => {
        let params = {
          good_code_name: this.ruleForm.good_code_name,
          parent_cate_id: this.ruleForm.parent_cate_id + '',
          sort: this.ruleForm.sort
        }
        if (valid) {
          const detail = await addLiangList(params)
          if (detail.code === 200) {
            this.$message.success('新增成功')
            this.visible = false
            this.$emit('updateTable')
          }
        } else {

          return false;
        }
      });
    },
    handleClose(done) {
      this.visible = false
      Object.keys(this.ruleForm).map(key => {
        this.ruleForm[key] = null
      })
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">

.app-container /deep/ .el-date-editor.el-input{
  margin-bottom: 10px;
}

</style>
